package com.zl.realm;


import org.apache.shiro.authc.*;
import org.apache.shiro.realm.Realm;

/**
 * @author ZLL
 * @date create in 2018/05/10
 */
public class MyRealm implements Realm{
    @Override
    public String getName() {
        return "my-realm";
    }

    @Override
    public boolean supports(AuthenticationToken authenticationToken) {
        return authenticationToken instanceof UsernamePasswordToken;
    }

    @Override
    public AuthenticationInfo getAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {


        String username = (String) authenticationToken.getPrincipal();

        String password = (String) authenticationToken.getCredentials();

        if(!"zhang".equals(username)){
            throw new UnknownAccountException("用户名不合法");
        }

        if(!"123".equals(password)){
            throw new IncorrectCredentialsException("密码错误");
        }

        return new SimpleAuthenticationInfo(username, password, getName());
    }
}
